package com.wbdgj.ui.home;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.google.gson.internal.LinkedTreeMap;
import com.wbdgj.R;
import com.wbdgj.adapter.MessageAdapter;
import com.wbdgj.adapter.WbAdapter;
import com.wbdgj.base.BaseActivity;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultMessageObjectModel;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.ui.near.MessageBoardDetailActivity;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;
import com.wbdgj.utils.refresh.PullListView;
import com.wbdgj.utils.refresh.PullToRefreshLayout;

import java.text.DecimalFormat;
import java.util.ArrayList;

import butterknife.BindView;
import retrofit2.Call;

public class MessageListActivity extends BaseActivity{

    private Context context = this;

    @BindView(R.id.demo_listview)
    PullListView myPullListView;
    @BindView(R.id.pullToRefreshLayout)
    PullToRefreshLayout myPullToRefreshLayout;
    private int page = 1;
    private int totalPage = 10;
    private int total;
    private ArrayList<LinkedTreeMap> linkedTreeMapList;
    private MessageAdapter messageAdapter;
    private DecimalFormat decimalFormat;

    private String id;

    //声明AMapLocationClient类对象
    public AMapLocationClient mLocationClient = null;
    //声明AMapLocationClientOption对象
    public AMapLocationClientOption mLocationOption = null;

    @Override
    protected int getLayoutResId() {
        return R.layout.ui_message_list;
    }

    @Override
    protected void initView() {
        id = getIntent().getStringExtra("id");
        listAll();
        myPullToRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        listAll();
                        myPullToRefreshLayout.refreshFinish(true);
                        ToastUtils.toastShort("刷新成功");
                    }
                }, 1000);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                page = page + 1;
                if (((page-1)*totalPage) > total){
                    ToastUtils.toastShort("已无更多");
                }else{
                    listAll();
                }
                myPullToRefreshLayout.loadMoreFinish(true);
            }
        });

        myPullListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (!messageAdapter.hasNoData){
                    BaseApplication.getInstance().setLinkedTreeMap(messageAdapter.getItem(i));
                    startActivity(new Intent(context, MessageBoardDetailActivity.class).putExtra("branch_id", id));
                }
            }
        });
    }




    private void listAll() {
        Call<ResultMessageObjectModel> call = HttpAdapter.getSerives().msgList("", ""+totalPage, ""+page,id, "", "", "3" );
        call.enqueue(new OnResponseListener<ResultMessageObjectModel>(context) {
            @Override
            protected void onSuccess(ResultMessageObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getCode() == 200){
                    linkedTreeMapList = (ArrayList<LinkedTreeMap>) userModelBaseResultModel.getData();
                    total = userModelBaseResultModel.getTotal();
                    if (page == 1){
                        messageAdapter = new MessageAdapter(context, linkedTreeMapList);
                        myPullListView.setAdapter(messageAdapter);
                    }else{
                        messageAdapter.loadMoreListView(linkedTreeMapList);
                        messageAdapter.notifyDataSetChanged();
                    }
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getMsg());
                }
            }
        });
    }
}
